package parser;

import model.Arc;
import model.Graph;
import model.Noeud;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Thomas
 * Date: 06/02/14
 * Time: 15:59
 * Classe permettant la gestion des fichiers contenant les graphs
 */
public class ParserGraph {
    public static Graph parse(String filename) throws IOException {
        Graph graph = new Graph();

        FileReader fileInputStream = new FileReader(filename);
        BufferedReader br=new BufferedReader(fileInputStream);
        String ligne;
        ligne = br.readLine();
        String[] arguments = ligne.split(" ");
        int nb_noeud = Integer.parseInt(arguments[0]);
        int nb_arc = Integer.parseInt(arguments[1]);

        int i = 0;
        while ((ligne = br.readLine()) != null){
            if (i < nb_noeud) {
                //lecture des noeuds
                String[] arguments_noeud = ligne.split(" ",2);
                int id_noeud = Integer.parseInt(arguments_noeud[0]);
                Noeud noeud = new Noeud(id_noeud,arguments_noeud[1]);

                graph.addNoeud(noeud);
            } else {
                //lecture des arcs
                String[] arguments_arc = ligne.split(" ", 4);
                int id_noeud_source = Integer.parseInt(arguments_arc[0]);
                int id_noeud_destination = Integer.parseInt(arguments_arc[1]);
                double cout = Double.parseDouble(arguments_arc[2]);
                String ligne_bus = arguments_arc[3];
                Arc arc = new Arc(graph.getNoeuds().get(id_noeud_source),
                        graph.getNoeuds().get(id_noeud_destination),
                        ligne_bus,
                        cout);

                graph.getNoeuds().get(id_noeud_source).getNext().add(arc);
                graph.addArc(arc);
            }
            i++;
        }
        br.close();

        return graph;
    }

    public static void save(String filename, Graph graph) throws FileNotFoundException, UnsupportedEncodingException {
        PrintWriter writer = new PrintWriter(filename, "UTF-8");

        HashMap<String, ArrayList<Arc>> arcs = graph.getArcs();
        HashMap<Integer, Noeud> noeuds = graph.getNoeuds();

        writer.println(noeuds.size() + " " + arcs.size());

        for(Map.Entry<Integer, Noeud> entry : noeuds.entrySet()) {
            Noeud value = entry.getValue();
            writer.println(value.getId() + " " + value.getName());
        }

        for(Map.Entry<String, ArrayList<Arc>> entry : arcs.entrySet()) {
            String ligne = entry.getKey();
            ArrayList<Arc> value = entry.getValue();
            for (Arc arc : value) {
                writer.println(arc.getSource().getId() + " " + arc.getDestination().getId() + " " + arc.getCout() + " " + ligne);
            }
        }

        writer.close();
    }
}
